package com.maycari.captainbi.scheduled;

import com.alibaba.fastjson.JSONArray;
import com.maycari.captainbi.data.dto.AuthorizationManagementDto;
import com.maycari.captainbi.service.AuthorizationManagementService;
import com.maycari.captainbi.service.Captain1Service;
import com.maycari.captainbi.util.MaycariRedis;
import com.maycari.tool.utils.CollectionUtil;
import com.maycari.tool.utils.DateUtil;
import com.maycari.tool.utils.SpringUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: 王宏伟
 * @DateTime: 2021/4/12 15:27
 * @Description: TODO
 */
@Component
@Slf4j
public class SyncRecordTask {

    @Autowired
    private MaycariRedis maycariRedis;

    @Autowired
    private AuthorizationManagementService authorizationManagementService;

    private static String dataKey = "MAYCARI:DATAKEY:";

//    private List<String> list = Arrays.asList(new String[]{
//            "captain1ServiceImpl", "captain2ServiceImpl", "captain3ServiceImpl", "captain4ServiceImpl", "captain5ServiceImpl"
//            , "captain7ServiceImpl", "captain8ServiceImpl", "captain9ServiceImpl"
//    });

    private List<String> list = Arrays.asList(new String[]{
            "46"
    });

    @XxlJob("syncRecordTask")
    public ReturnT<String> execute(String param) {
        List<AuthorizationManagementDto> all = authorizationManagementService.getAll();
        if (CollectionUtil.isNotEmpty(all)) {


            for (AuthorizationManagementDto dto : all) {
                String siteName = dto.getSiteName();
                Date start_time = maycariRedis.get(dataKey + siteName);
                if (start_time == null) {
                    //往前推一个月数据
                    Date date = DateUtil.minusMonths(dto.getCreateTime(), 1);
                    start_time = DateUtil.parse(DateUtil.format(date, DateUtil.PATTERN_DATE) + " 00:00:00", DateUtil.PATTERN_DATETIME);
                }

                Date now = new Date();

                while (true) {
                    //一天
                    Date end_time = DateUtil.minusSeconds(DateUtil.plusDays(start_time, 1), 1);
                    if (end_time.after(now)) {
                        break;
                    }
                    sendMq(dto.getId(), start_time, end_time);
                    start_time = DateUtil.plusSeconds(end_time, 1);
                }
                maycariRedis.set(dataKey + siteName, start_time);
            }

        }
        return ReturnT.SUCCESS;
    }

    private void sendMq(Integer id, Date start_time, Date end_time) {
        list.forEach(item -> {
            try {
                Object bean = SpringUtil.getBean("captain" + item + "ServiceImpl");
                Method me = bean.getClass().getDeclaredMethod("sync", Integer.class, Date.class, Date.class);
                me.invoke(bean, id, start_time, end_time);
            } catch (Exception e) {
                log.error("发生了异常", e);
            }
        });
    }
}
